package jface;

import jface.data.Company;
import jface.data.Person;
import jface.data.Person.Gender;
import jface.util.PersonIconLabelProvider;
import jface.util.PersonTreeContentProvider;

import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class JFaceTreeImageExample {
	
	public static ImageRegistry imageRegistry;
	
	public static void main(String[] args) {		
		Display display = new Display();
		
		String path = System.getProperty("user.dir") + "/images/";
		
		
		imageRegistry = new ImageRegistry(display);

		Image img = new Image(display, path + "manager.png");
		img = new Image(display, img.getImageData().scaledTo(20, 20));
		imageRegistry.put("manager", img);
		
		img = new Image(display, path + "minion_female.png");
		img = new Image(display, img.getImageData().scaledTo(20, 20));
		imageRegistry.put("minion_f", img);
		
		img = new Image(display, path + "minion_male.png");
		img = new Image(display, img.getImageData().scaledTo(20, 20));
		imageRegistry.put("minion_w", img);
			
		Company company = new Company();
		Person managerA = new Person("Mike", "Manager", 28, Gender.male);
		for (int i = 0; i < 6; i++) {
			managerA.getEmployees().add(new Person("Scott", "Minion"+i, (i+1)*7, i > 3 ? Gender.female : Gender.male));
		}
		Person managerB = new Person("Jim", "Boss", 28, Gender.male);
		for (int i = 0; i < 6; i++) {
			managerB.getEmployees().add(new Person("Tim", "Minion"+i, (i+2)*7, i > 3 ? Gender.female : Gender.male));
		}
		company.getEmployees().add(managerA);
		company.getEmployees().add(managerB);
		
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		
		TreeViewer treeViewer = new TreeViewer(shell);

		treeViewer.setContentProvider(new PersonTreeContentProvider());
		treeViewer.setLabelProvider(new PersonIconLabelProvider());
		treeViewer.setInput(company);
		
		treeViewer.getTree().pack();
		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		display.dispose();
	}

}
